﻿using DB.EF;
using EF.DB;
using Microsoft.EntityFrameworkCore;
using Project.Utils;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using VOL.Core.ManageUser;
using VOL.Entity.DomainModels;
using static Dapper.SqlMapper;

namespace Orther.Service
{
    public class BaseService<T> where T : class
    {
        public MysqlContext mysqlContext = new MysqlContext();
        //GenericRepository<T> repository;
        public BaseService()
        {
            //repository = new GenericRepository<T>(mysqlContext);
        }

        public bool deleteFrom(string tableName,string idKey,Guid id_Value, operatelog oLog)
        {
            try
            {
                Action myAction = () =>
                {
                    string sql = "delete from " + tableName + " where " + idKey + " = @p0";

                    //var parameters = new[]
                    //{
                    //    new SqlParameter("@idValue", id_Value.ToString())
                    //};

                    mysqlContext.Database.ExecuteSqlRaw(sql, id_Value);
                    mysqlContext.Set<operatelog>().Add(oLog);
                    //最后添加SaveChanges
                    mysqlContext.SaveChanges();
                   
                };

                var transactionWrapper = new TransactionWrapper(mysqlContext);
                transactionWrapper.UseTransaction(myAction);
                return true;
            }
            catch (Exception ex) {
                throw ex;
                return false;
            }
            

        }


        public bool SaveForm(T entity, operatelog oLog, List<object[]> defaultCols = null)
        {
            try
            {
                entity = EntityUtils.SetDefuatValueT(entity,defaultCols);

                Action myAction = () =>
                {
                    //第一步：添加数据操作
                    mysqlContext.Set<T>().Add(entity);
                    //第二步：记录日志
                    mysqlContext.Set<operatelog>().Add(oLog);
                    //最后添加SaveChanges
                    mysqlContext.SaveChanges();
                };

                var transactionWrapper = new TransactionWrapper(mysqlContext);
                transactionWrapper.UseTransaction(myAction);
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
                return false;
            }
        }

        public bool UpdateForm(T entity, operatelog oLog, List<object[]> defaultCols = null)
        {
            try
            {
                entity = EntityUtils.SetDefuatValueT(entity, defaultCols);

                Action myAction = () =>
                {
                    //第一步：添加数据操作
                    var entry = mysqlContext.Entry(entity);
                    entry.State = EntityState.Modified;
                    //第二步：记录日志
                    mysqlContext.Set<operatelog>().Add(oLog);
                    //最后添加SaveChanges
                    mysqlContext.SaveChanges();
                };

                var transactionWrapper = new TransactionWrapper(mysqlContext);
                transactionWrapper.UseTransaction(myAction);
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
                return false;
            }
        }


        public bool UpdateForm(T entity, List<object[]> defaultCols = null)
        {
            try
            {
                Action myAction = () =>
                {
                    //第一步:密钥信息更新
                    entity = EntityUtils.SetDefuatValueT(entity,defaultCols);
                    var entry = mysqlContext.Entry(entity);
                    entry.State = EntityState.Modified;
                    //第二步:记录日志
                    operatelog oLog = new operatelog();
                    List<object[]> logDefaultCols = new List<object[]>();
                    logDefaultCols.Add(new object[] { "CreateDate", defaultCols[0][1] });
                    logDefaultCols.Add(new object[] { "CreateID", defaultCols[1][1] });
                    logDefaultCols.Add(new object[] { "Creator", defaultCols[2][1] });
                    oLog = EntityUtils.SetDefuatValueT(oLog, logDefaultCols);
                    oLog.Log_Id= Guid.NewGuid();
                    oLog.Content = defaultCols[0][1] + " " + defaultCols[2][1] + "更新了一条密钥企业的信息，把用户状态设置为:【" + (defaultCols[3][1] + "" == "1" ? "不可用" : "可用") + "】。";
                    oLog.Category = "密钥用户设置是否可用";
                    oLog.MenuModel = "密钥生成管理";
                    mysqlContext.Set<operatelog>().Add(oLog);
                    //第三步:批量更新
                    mysqlContext.SaveChanges();
                };

                var transactionWrapper = new TransactionWrapper(mysqlContext);
                transactionWrapper.UseTransaction(myAction);
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
                return false;
            }
            
        }



    }
}
